package com.health.mapp.impl;

import io.rong.models.TokenReslut;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.health.commons.PropertiesService;
import com.health.db.model.UserLIST;
import com.health.db.model.UserLISTTemp;
import com.health.db.model.UserLoginJournal;
import com.health.db.service.msg.IUserLoginJournalService;
import com.health.db.service.sm.IRongService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.RegisterForPartnerReq;
import com.health.mapp.bean.RegisterForPartnerResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 注册接口
 * 		appId=12345679&version=1.0&method=register&timestamp=20121212154843&uuId=05ae94531ed53d89ff5da71d6f2ad780366a4eb9&memberid=1&sign=2976b2f3db9530e0de8097c1c912550f&lang=zh-Hans00000000000000000
 * 			{"userPhone":"13810570675","userName":"13810570675","password":"123456"}
 * @author HiRoad
 *
 */
public class RegisterForPartnerImpl implements ActionInterface {
	
	private IUserLISTService userLISTService  ;
	private IRongService rongService;
	private IUserLoginJournalService userLoginJournalService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		RegisterForPartnerReq req=(RegisterForPartnerReq) reqBean;
		//do it bobo
		RegisterForPartnerResp _resp= new RegisterForPartnerResp();
		
		if (StringUtils.isBlank(req.getUserPhone())){
			
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}


		//check the userid
		UserLIST user=userLISTService.selectByUserNo(req.getUserPhone());
		UserLIST  userLIST =new UserLIST();
		UserLISTTemp  returnUserBean =new UserLISTTemp();
		Date now =DateUtils.now();
		
		//可以注册
		if (null==user) {
			
			long userLoginResult=-1;
			
			//userLIST.setUserAlias("");
			userLIST.setUserName(req.getUserPhone());
			userLIST.setApptoken("");
			userLIST.setUserNo(req.getUserPhone());
			userLIST.setPasswordCreateDate(now);
			userLIST.setLoginDatetime(now);
			userLIST.setLogoutDatetime(now);
			userLIST.setPassword(null);
			userLIST.setCreateDate(now);
			userLIST.setUpdateDate(now);
			userLIST.setRegisterDate(now);
			//user :1 doctor:3
			userLIST.setTypeid(1);
			userLIST.setTypeName("用户");
			userLIST.setIsUsedFlag((short) 1);
			//user/userdefault.png日
			userLIST.setIconUrl(PropertiesService.getString("iconUrl.userDefaultUrl").trim());
			//userLIST.setRunstatusid((short)2);
			if (StringUtils.isNotBlank(req.getBusinessCode())) {
				userLIST.setComment(req.getBusinessCode());
			}
		
			//check the userid
			 userLoginResult=userLISTService.insertUser(userLIST);
				
			returnUserBean.setUserId(String.valueOf(userLIST.getUserid()));
			returnUserBean.setUserName(userLIST.getUserName());
			returnUserBean.setUserPhone(userLIST.getUserNo());
			returnUserBean.setRegTime(now);
			returnUserBean.setIcon(userLIST.getIconUrl());
			//先做唯一性校验
		}else{
				
			returnUserBean.setUserId(String.valueOf(user.getUserid()));
			returnUserBean.setUserName(user.getUserName());
			returnUserBean.setUserPhone(user.getUserNo());
			returnUserBean.setRegTime(now);
			returnUserBean.setIcon(user.getIconUrl());
				
		}
			/**
			 * token为空的话重新从融云获取Token
			 * */
			TokenReslut tokenReslut=rongService.getUserToken(String.valueOf(returnUserBean.getUserId()), returnUserBean.getUserName(), PropertiesService.getString("iconUrl.userDefaultUrl").trim());
			if(null!=tokenReslut&&tokenReslut.getCode()==200)
			{
				_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
				_resp.setErrorMsg("Success");
				_resp.setToken(tokenReslut.getToken());
				
			}else {
				_resp.setErrorCode(-1);
				_resp.setErrorMsg("获取Token失败");
			}
		 
		 
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
		
		_resp.setUser(returnUserBean);
		
		UserLoginJournal loginJournal = new UserLoginJournal();

		loginJournal.setUserid(Long.valueOf(returnUserBean.getUserId()));
		loginJournal.setMachinedeviceid(header.getUuId());
		loginJournal.setLoginDatetime(DateUtils.now());

		userLoginJournalService.insert(loginJournal);
		
		// TODO Auto-generated method stub
		return _resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the rongService
	 */
	public IRongService getRongService() {
		return rongService;
	}
	/**
	 * @param rongService the rongService to set
	 */
	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}
	
	public IUserLoginJournalService getUserLoginJournalService() {
		return userLoginJournalService;
	}
	public void setUserLoginJournalService(
			IUserLoginJournalService userLoginJournalService) {
		this.userLoginJournalService = userLoginJournalService;
	}
	
}
